﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsAsrsNetInput
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/7 16:43:41
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunWMS.SpiService;

internal class BWcsAsrsCrnInput : IWcsAsrsCrnInput
{

    private readonly static IRcsEquip _RcsEquip = new BRcsEquip();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();

    public List<AsrsCrnInputBean> GetAsrsCrnInput()
    {
        var db = Db.Default;

        var wcsAsrsCrnInputs = db.WcsAsrsCrnInputs.Select(t => t).OrderByDescending(t => t.Id).ToList();

        var areaIds = wcsAsrsCrnInputs.Select(t => t.AreaId).ToList();
        var crnIds = wcsAsrsCrnInputs.Select(t => t.CrnId).ToList();
        var equipIds = areaIds.Concat(crnIds).ToList();

        var inputLoc1s = wcsAsrsCrnInputs.Select(t => t.InputLoc1).ToList();
        var inputLoc2s = wcsAsrsCrnInputs.Select(t => t.InputLoc2).ToList();
        var inputIds = inputLoc1s.Concat(inputLoc2s).ToList();


        var equips = _RcsEquip.GetRcsEquipByIds(equipIds);
        var locs = _RcsLocation.GetLocationsByLocIds(inputIds);

        var result = wcsAsrsCrnInputs.Join(equips, f => f.AreaId, s => s.Id, (f, s) => new
        {
            Input = f,
            Area = s,
        }).Join(equips, f => f.Input.CrnId, s => s.Id, (f, s) => new
        {
            f.Input,
            f.Area,
            Crn = s,
        }).Join(locs, f => f.Input.InputLoc1, s => s.Id, (f, s) => new
        {
            f.Input,
            f.Area,
            f.Crn,
            InputLoc1 = s,
        }).Join(locs, f => f.Input.InputLoc2, s => s.Id, (f, s) => new
        {
            f.Input,
            f.Area,
            f.Crn,
            f.InputLoc1,
            inputLoc2 = s,
        }).Select(t => new AsrsCrnInputBean
        {
            Id = t.Input.Id.GetValueOrDefault(),
            AreaName = t.Area.EquipName,
            CrnName = t.Crn.EquipName,
            CrnCode = t.Crn.Id,
            InputLoc1 = t.InputLoc1.LocName,
            InputLoc2 = t.inputLoc2.LocName,
            Number = t.Input.TaskCount.GetValueOrDefault(),
            Reason = t.Input.Reason,
            EquipEnable = t.Crn.EquipEnable
        }).ToList();

        return result;
    }
    public List<WcsAsrsCrnCountBean> GetAsrsCrnInputV2()
    {
        var db = Db.Default;

        var inresult = db.WcsAsrsCrnInputs.Join(db.RcsEquipCrns, f => f.CrnId, s => s.Id, (f, s) => new
        {
            WcsAsrsCrnInputs = f,
            s.WarehourseId
        }).Join(db.RcsEquipWarehouses, f => f.WarehourseId, s => s.Id, (f, s) => new
        {
            f.WcsAsrsCrnInputs,
            s.WarehouseName
        }).Select(t => new WcsAsrsCrnCountBean
        {
            Id = t.WcsAsrsCrnInputs.Id ?? 0,
            Number = t.WcsAsrsCrnInputs.TaskCount ?? 0,
            ShowName = t.WcsAsrsCrnInputs.ShowName,
            AreaId = t.WcsAsrsCrnInputs.AreaId ?? 0,
            WarehoseName = t.WarehouseName,
            Type = 1,
        }).ToList();


        var outResult = db.WcsAsrsCrnOutputs.Join(db.RcsEquipCrns, f => f.CrnId, s => s.Id, (f, s) => new
        {
            WcsAsrsCrnOutputs = f,
            s.WarehourseId
        }).Join(db.RcsEquipWarehouses, f => f.WarehourseId, s => s.Id, (f, s) => new
        {
            f.WcsAsrsCrnOutputs,
            s.WarehouseName
        }).Select(t => new WcsAsrsCrnCountBean
        {
            Id = t.WcsAsrsCrnOutputs.Id ?? 0,
            Number = t.WcsAsrsCrnOutputs.TaskCount ?? 0,
            ShowName = t.WcsAsrsCrnOutputs.ShowName,
            AreaId = t.WcsAsrsCrnOutputs.AreaId ?? 0,
            WarehoseName = t.WarehouseName,
            Type = 2,
        }).ToList();

        var result=inresult.Union(outResult).ToList();
        return result;

    }
    public int UpdateAsrsCrnInputCount(long id, int number, string reason)
    {
        var db = Db.Default;

        var result = db.WcsAsrsCrnInputs.Where(t => t.Id == id).Update(new POWcsAsrsCrnInput
        {
            TaskCount = number,
            UpdateTime = DateTime.Now,
            Reason = reason
        });

        return result;
    }
}


